* Jon Temple and Nicolas Van de Sijpe, 29-May-2017

/* READ ME: 
- This file generates most of the results reproted in the tables in Temple and Van de Sijpe, Foreign aid and domestic absorption.
The code below would take a while to run if you run it all at once. Therefore, we have 
always run the code block by block to generate results, by uncommenting 
the relevant block of code and running the do-file, then commenting it out again, 
uncommenting the next block of code, running the do-file, and so on. 
This also makes sure that there are no stored results, variables etc. hanging around from previous estimations,
which may prevent a block of code to run otherwise.
Hence, to generate a particular result, uncomment the relevant block of code below and run the do-file.
- You will have to adjust paths below to run the code. Relative paths should be clear from the code.
- Further guidance is given below, in comments. 
*/


*************************************************************
* SET UP DO-FILE
*************************************************************
version 14
clear all
set more off, perm
set matsize 2000
set emptycells drop

local aidinstrument "C:\Users\ec1nv\Dropbox\My Documents\My Papers\Aid and absorption" // change this local to run the code yourself.
local aidinstrumentdata "`aidinstrument'\Data"
local ouranalysis "`aidinstrumentdata'\Results\Our analysis"
local regrtables "`aidinstrument'\Paper\Tables"

/* Include do-file with "storeresult" program. 
This is a little program that automatically picks up the statistics we want to display
so that we can easily make tables and export to TeX. */ 
qui include "`ouranalysis'\storeresult.do"


********************************************************************
* DEFINE MACROS TO DISPLAY AND EXPORT RESULTS LATER ON
********************************************************************
local exportstata b(a3) se(a3) star(* 0.10 ** 0.05 *** 0.01) nonotes obslast ///
scalar(LReffect LReffect_se LReffect_p "countries Countries") sfmt(a3 a3 %05.3f %8.0g) 

local exportstataIV b(a3) se(a3) star(* 0.10 ** 0.05 *** 0.01) nonotes obslast /// 
scalar(LReffect LReffect_se LReffect_p weakid_Fstat underid_stat underid_p "countries Countries") /// 
sfmt(a3 a3 %05.3f a3 a3 %05.3f %8.0g)

local exportstataIVrob b(a3) se(a3) star(* 0.10 ** 0.05 *** 0.01) nonotes obslast /// 
scalar(LReffect LReffect_se LReffect_p weakid_Fstat "countries Countries") sfmt(a3 a3 %05.3f a3 %8.0g)


*************************************************************
* SET UP DATA
*************************************************************
use "`ouranalysis'\JIE data.dta"
d, fulln
su ccode

sort ccode periodid
xtset ccode periodid

// generate time dummies.
su periodid
local periodidmax = r(max)
foreach num of numlist 1(1)`periodidmax' {
	gen ydummy`num'=(periodid==`num')
}
*browse country ccode period periodid ydummy*


*************************************************************
* RENAME VARIABLES
*************************************************************
/* Give slightly shorter names to dependent variables -- easier to use later on in loops. */
rename househconsGDP hconsGDP
rename govtconsGDP gconsGDP
rename finalconsGDPsum tconsGDPsum 
rename grosscapformGDP grcapformGDP

d *end, fulln
foreach endname in _end {
	rename househconsGDP`endname' hconsGDP`endname'
	rename govtconsGDP`endname' gconsGDP`endname'
	rename finalconsGDPsum`endname' tconsGDPsum`endname' 
	rename grosscapformGDP`endname' grcapformGDP`endname'
}
// generate sum of C, I and G
gen double cigGDPsum = tconsGDPsum + grcapformGDP
// generate log of total consumption and C+I+G
su tconsGDPsum
gen double lntconsGDPsum = ln(tconsGDPsum)
su lntconsGDPsum, detail
gen double lncigGDPsum = ln(cigGDPsum)
su 


*************************************************************
* SELECT AID VARIABLE
*************************************************************
gen double aidvar = netaidGDP
d netaidGDP aidvar, fulln
label variable aidvar "Aid"


*************************************************************
* CHOOSE DEPENDENT VAR AND AID INSTRUMENT
*************************************************************
gen double depvar = grcapformGDP
label variable depvar "Dependent variable"
gen double lagdepvar = L.depvar
label variable lagdepvar "Lagged dep. variable"
gen double aidiv = netaidinstr6070GDP


// Uncomment if you want to run this block of code. Same goes for any block of code below that is commented out.
/* 
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 2 small countries included", text replace
// Put a folder called "Result logs" in your main folder if you want to save log files.
******************************************************************
* TABLE 8 ROW 2: ROBUSTNESS CHECK WITH SMALL COUNTRIES INCLUDED 
******************************************************************
/* Small countries not dropped yet here so they will be included. */

// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Need at least 6 time series observations for estimation. Each country has constant (FE) plus 4 factor loadings 
		(aid, instrument, dep var and lag dep var). */

		// construct cross-sectional means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)' // use program storeresult to pick up relevant statistics
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP, ///
keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
// 
timer clear
log close
*/


*************************************************************
* DROP SMALL COUNTRIES
*************************************************************
/* First generate sample indicator by country. Use dynamic CCE IV for this: */
capture drop incflag
gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
capture drop incflagsum
bysort ccode (period): egen incflagsum = sum(incflag)
*browse ccode periodid incflag incflagsum
capture drop sampleobs
gen sampleobs=(incflag == 1 & incflagsum >= 6) // 1 for an observation that is included in the sample.
label variable sampleobs "1 if observation is included in full sample"
capture drop samplecountry
gen samplecountry=(incflagsum >= 6) // 1 for a country that is included in the sample.
label variable samplecountry "1 if country is included in full sample"
// Check:
tab sampleobs, missing
tab samplecountry if periodid == 1, missing

/* Now identify small countries */
d population, fulln
sort ccode periodid
*browse ccode periodid population
/* Designate countries as small if fewer than 500.000 people or population missing.
Use first period in estimation sample for this (1971-73). */
gen small1=((population < 500000 | population == .) & period == "1971-73")
*browse ccode periodid population small1
tab small1 if period == "1971-73", missing
bysort ccode (periodid): egen small = max(small1)
*browse ccode periodid population small1 small
label variable small "1 if small country"

/* List countries; whether or not they would be in full sample; and whether or not they are small. */
list country samplecountry small population if periodid == 5
// These are the countries that would be in the sample but get dropped because they are too small:
list country samplecountry small population if periodid == 5 & samplecountry == 1 & small == 1
*browse country period samplecountry sampleobs small population grcapformGDP lagdepvar netaidinstr6070GDP netaidGDP

/* Now drop small countries. */
drop if small == 1

tab incflag if incflagsum >= 6, missing // 1099 observations left, 88 countries.

/* Check these in a bit more detail. */
count if incflag == 1 & incflagsum >= 6
tab incflagsum if incflag == 1 & incflagsum >= 6, missing
gen countryfullTSraw = 1 if incflagsum == 14 | incflagsum == 13
replace countryfullTSraw = 0 if countryfullTSraw == .
tab countryfullTSraw if incflagsum >= 6 & periodid == 18
// 57 of 88 (64.77%) countries have 13 or 14 observations.
*browse country period periodid incflag incflagsum countryfullTSraw
drop countryfullTSraw


*****************************************************************
* CHECK IDENTITY GDP = C + I + G + X - M
*****************************************************************
/* Note for here this implies that C/GDP + I/GDP + G/GDP + X/GDP - M/GDP = 100 (as all variables expressed as percentages of GDP). */
//d, fulln
gen double identsum = hconsGDP + grcapformGDP + gconsGDP + exportGDP - importGDP
su identsum, detail // most of these are exactly 100. Few discrepancies; and small when they do occur.
su identsum hconsGDP grcapformGDP gconsGDP exportGDP importGDP

// Zoom in on the sample used in estimation.
tab sampleobs, missing
tab samplecountry if periodid == 1, missing

/* Now check discrepancy in sample. */
capture drop identsum
gen double identsum = hconsGDP + grcapformGDP + gconsGDP + exportGDP - importGDP if sampleobs == 1
su identsum, detail // most of these are exactly 100. Very few discrepancies; and small when they do occur.
su identsum hconsGDP grcapformGDP gconsGDP exportGDP importGDP if sampleobs == 1
/* Identify countries with discrepancies over 1\% of GDP. */
gen identsum_discr = 1 if identsum != . & (identsum >= 101 | identsum <= 99)
label variable identsum_discr "Discrepancy in GDP identity of more than 1% of GDP"
count if identsum_discr == 1 // 1 observation out of 1099 has discrepancy larger than 1% of GDP.
list country period identsum hconsGDP grcapformGDP gconsGDP exportGDP importGDP if identsum_discr == 1
// Mali 2004-06.


*************************************************************
* EXTRA VARIABLES TO CHECK KRONMAL PROBLEM LATER ON
*************************************************************
su invGDPcurrentUSD, detail
replace invGDPcurrentUSD = invGDPcurrentUSD*1000000
label variable invGDPcurrentUSD "1 over current US$ GDP, times 1000000"

su GDPcurrentUSD, detail
gen double lnGDPcurrentUSD = ln(GDPcurrentUSD)
su lnGDPcurrentUSD, detail


*********************************************************************
* IDENTIFY COUNTRIES WITH LARGEST RELATIVE DROP IN REAL GDP
*********************************************************************
/* To later drop, in figures in appendix. */
// First zoom in on the sample used in estimation.
tab sampleobs, missing
tab samplecountry if periodid == 1, missing

// Now generate relative real GDP changes but only for observations in the sample:
d GDPconstantLCU
bysort ccode (periodid): gen double relRGDPchange = 100*((GDPconstantLCU[_n] - GDPconstantLCU[_n-1])/GDPconstantLCU[_n-1]) if sampleobs == 1
// Pick up minimum (most negative) percentage change in real GDP for each country, calculated over sample observations:
bysort ccode (periodid): egen double minrelRGDPchange1 = min(relRGDPchange) if sampleobs == 1
bysort ccode (periodid): egen double minrelRGDPchange = mean(minrelRGDPchange1) if samplecountry == 1
// latter is same variable but showing up in every period for countries that are in the sample.
drop minrelRGDPchange1
gsort +minrelRGDPchange +periodid
capture drop rank
gen rank = _n if minrelRGDPchange != .
su periodid
local numberperiods = r(max)
gen minrelRGDPchangerank = ceil(rank/`numberperiods')
label variable minrelRGDPchangerank "Rank from most negative to least negative percentage change in real LCU GDP"
*browse ccode period periodid incflag incflagsum GDPconstantLCU relRGDPchange minrelRGDPchange minrelRGDPchangerank
drop rank
// Check what distribution of most negative changes looks like:
su minrelRGDPchange if periodid == 18, detail


*************************************************************
* CHECK INDEPENDENCE YEARS
*************************************************************
// Check:
tab sampleobs, missing
tab samplecountry if periodid == 1, missing

/* List countries; whether or not they would be in full sample; and independence year. */
list country samplecountry indepyear_polIV if periodid == 1
// independence years for countries in sample:
tab indepyear_polIV if periodid == 1 & samplecountry == 1, missing
// list those countries in sample with independence post-1960:
list country samplecountry indepyear_polIV if periodid == 1 & samplecountry == 1 & indepyear_polIV > 1960
*browse country period indepyear_polIV netaidGDP netaidinstr6070GDP indep_netaidGDP indep_netaidinstr6070GDP if samplecountry == 1 & indepyear_polIV > 1960


*************************************************************
* GENERATE OBSERVATION NUMBER
*************************************************************
// will be useful when exporting to other software (e.g. R to identify outliers)
sort ccode periodid
xtset ccode periodid
gen obsnum = _n
order country ccode period periodid obsnum
*browse


// Uncomment if you want to run this block of code.
/*
********************************************************************************************************
* CHECK WHETHER PARTIALLING OUT GIVES SAME RESULT
********************************************************************************************************
/* The variance-covariance matrices estimated below are rank deficient because 
of the large number of estimated parameters relative to the number of clusters (countries).
This block of code shows that we obtain the same parameter estimates and standard error for the coefficient
on the aid variable if we first partial out fixed effects and cross-sectional means. */

// Set up, and restrict to sample used in dynamice CCE IV:
sort ccode periodid
replace aidvar = netaidGDP
replace depvar = grcapformGDP
replace lagdepvar = L.depvar
replace aidiv = netaidinstr6070GDP
capture drop incflag
gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
capture drop incflagsum
bysort ccode (period): egen incflagsum = sum(incflag)
// construct cross-sectional means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6

* Static CCE IV regression -- this is the result from the basic model below.
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1 & incflagsum >= 6, ///
fe cluster(ccode) endog(aidvar) first savefirst small

* Check we get the same result when partialling out:
capture drop depvarres aidres ivres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double ivres, e
xtivreg2 depvarres (aidres = ivres) if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidres) first savefirst small
// FS and SS coefficients the same but SEs lower, because dof used up are undercounted here.

/* Now get SEs of partialling out estimation to match up with those in original estimation. 
Partialling out means that many estimated parameters are not counted as detracting from the dof of the estimated model.
Hence, the small sample adjustment is less costly and you get smaller SEs.
But we can partial out and manually adjust the SEs to match up again with those reported above by xtivreg2, small. */
* Repeat static CCE IV regression -- this is the result from the basic model below.
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1 & incflagsum >= 6, ///
fe cluster(ccode) endog(aidvar) first savefirst small
scalar mdof = e(df_m) // pick up number of estimated parameters (excludes fixed effects)
di mdof
* Now check we get the same when partialling out:
capture drop depvarres aidres ivres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
predict double ivres, e
xtivreg2 depvarres (aidres = ivres) if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidres) first savefirst small
// same as above: FS and SS coefficients the same but SE lower.
// now without small option (easier to start from to do dof correction manually)
qui xtivreg2 depvarres (aidres = ivres) if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidres) first savefirst
scalar clusters = e(N_g)
scalar numobs = e(N)
scalar dof_adj = sqrt((clusters/(clusters-1))*((numobs-1)/(numobs-mdof)))
scalar dof_adj_se = _se[aidres]*dof_adj
di dof_adj_se
/* This matches the SE from estimating the original model with xtivreg2, small. */
capture drop depvarres aidres ivres
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Tables 1-7 main results", text replace
*************************************************************
* TABLES 1-7 MAIN RESULTS 
*************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		/* Cross-sectional means: */
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		// static FE regression with time dummies:
		eststo FEstat: xtreg depvar aidvar ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode)
		qui storeresult `e(_estimates_name)'
		// dynamic FE regression with time dummies:
		eststo FEdyn: xtreg depvar lagdepvar aidvar ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode)
		qui storeresult `e(_estimates_name)'
		
		// static FE IV regression with time dummies:
		eststo FEIVstat: xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
		qui storeresult `e(_estimates_name)'
		// dynamic FE IV regression with time dummies:
		eststo FEIVdyn: xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, /// 
		fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		// static CCE regression
		eststo CCEstat: xi: xtreg depvar aidvar i.ccode|mdepvar i.ccode|maid if incflag == 1 & incflagsum >= 6, fe cluster(ccode) 
		qui storeresult `e(_estimates_name)'
		/* static CCE IV regression: */
		eststo CCEIVstat: xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		// dynamic CCE regression
		eststo CCEdyn: xi: xtreg depvar lagdepvar aidvar i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar if incflag == 1 & incflagsum >= 6, fe cluster(ccode) 
		qui storeresult `e(_estimates_name)'
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn: xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui

	/* DISPLAY RESULTS. */
	display "DEPENDENT VARIABLE IS `depvar'"
	esttab FEstat FEdyn CCEstat CCEdyn, keep(aidvar lagdepvar) mtitles(FE FE CCE CCE) `exportstata'
	esttab FEIVstat FEIVdyn CCEIVstat CCEIVdyn, keep(aidvar lagdepvar) mtitles("FE IV" "FE IV" "CCE IV" "CCE IV") `exportstataIV'
}
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
// 
timer clear
// check fraction of observations with zero aid and zero aid instrument.
count if incflag == 1 & incflagsum >= 6 // total number of obs.
scalar numobs = r(N)
count if incflag == 1 & incflagsum >= 6 & netaidGDP == 0 // number of obs. with zero aid
scalar zeroaid = r(N)
count if incflag == 1 & incflagsum >= 6 & netaidinstr6070GDP == 0 // number of obs. with zero aid instr
scalar zeroaidinstr = r(N)
count if incflag == 1 & incflagsum >= 6 & netaidGDP == 0 & netaidinstr6070GDP == 0 // number of obs. with zero aid and zero instr
scalar zeroaidandinstr = r(N)
// fraction of zeroes
di "fraction of observations zith zero aid: " zeroaid/numobs
di "fraction of observations zith zero aid instrument: " zeroaidinstr/numobs
di "fraction of observations zith zero aid and zero instrument: " zeroaidandinstr/numobs
/* Same for negative amounts: */
count if incflag == 1 & incflagsum >= 6 & netaidGDP < 0 // number of obs. with negative aid
scalar negaid = r(N)
count if incflag == 1 & incflagsum >= 6 & netaidinstr6070GDP < 0 // number of obs. with negative aid instr; should be zero
scalar negaidinstr = r(N)
// fraction of negative values:
di "fraction of observations with negative aid: " negaid/numobs
/* Full sample: */
//list country period if incflag == 1 & incflagsum >= 6
/* Save dataset that identifies observations in basic sample.
I do this once and then comment out the code. */
/*
rename country recipient
keep recipient period sampleobs
save "`ouranalysis'\sampleobs.dta", replace
*/
log close
*/


*************************************************************
* BCa CONFIDENCE INTERVALS IN TABLES 1-7
* and
* APPENDIX TESTING EQUALITY OF COEFFICIENTS
*************************************************************
/* To generate the BCa confidence intervals in tables 1-7,
please run the separate do-file `bootstrap CCE IV SEs'. 
Running this file will also generate the tests for the equality of CCE IV and 
FE IV discussed in the online appendix. */


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 3 lagged aid replaces current aid", text replace
***********************************************************************
* TABLE 8 ROW 3: ROBUSTNESS CHECK LAGGED AID REPLACES CURRENT AID 
***********************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP
capture drop lagaidvar
capture drop lagaidiv
gen double lagaidvar = L.netaidGDP
gen double lagaidiv = L.netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & lagaidvar<. & lagaidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar
		capture drop mlagaid mlaginst 
		capture drop mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagaid=mean(lagaidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlaginst=mean(lagaidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (lagaidvar = lagaidiv) i.ccode|mdepvar i.ccode|mlagaid i.ccode|mlaginst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(lagaidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (lagaidvar = lagaidiv) i.ccode|mdepvar i.ccode|mlagaid i.ccode|mlagdepvar i.ccode|mlaginst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(lagaidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(lagaidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(lagaidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 4 average current and lagged aid", text replace
***********************************************************************
* TABLE 8 ROW 4: ROBUSTNESS CHECK AVERAGE OF LAGGED AND CURRENT AID 
***********************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = .
replace aidiv = .
*browse country period periodid netaidGDP aidvar netaidinstr6070GDP aidiv
su ccode
local ccodemin = r(min) 
local ccodemax = r(max)
qui forval ccode = `ccodemin'/`ccodemax' { // cycle over all countries/ccodes
	qui forval periodid = 5/18 { // loop over all periods in the sample (from 1971 onwards, period 5).
		local y1 = `periodid' - 1 
		local y2 = `periodid'
		su netaidGDP if inrange(periodid, `y1', `y2') & ccode == `ccode' 
		replace aidvar = r(mean) if periodid == `periodid' & ccode == `ccode' & r(N) == 2 
		// latter if constraint means mean is only calculated if both aid values are non-missing.
		su netaidinstr6070GDP if inrange(periodid, `y1', `y2') & ccode == `ccode' 
		replace aidiv = r(mean) if periodid == `periodid' & ccode == `ccode' & r(N) == 2
	}
} // end loop over countries/ccodes
*browse country period periodid netaidGDP aidvar netaidinstr6070GDP aidiv

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 5 exclude first period", text replace
******************************************************************************
* TABLE 8 ROW 5: ROBUSTNESS CHECK EXCLUDE FIRST PERIOD 
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 6 instr6073", text replace
*********************************************************************************
* TABLE 8 ROW 6: ROBUSTNESS CHECK INSTRUMENT BASED ON 1960-73 INITIAL SHARES
*********************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6073GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 7 instr6073 exclude 1 period", text replace
*******************************************************************************************************
* TABLE 8 ROW 7: ROBUSTNESS CHECK INSTRUMENT BASED ON 1960-73 INITIAL SHARES, EXCLUDE ONE PERIOD
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6073GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 8 donor max Herf below 025", text replace
*******************************************************************************************************
* TABLE 8 ROW 8: ROBUSTNESS CHECK ONLY USING DONORS WITH MAX HERFINDAHL BELOW 0.25
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070Herf25GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


/* TO GENERATE THE RESULTS IN TABLE 8 ROW 9 WE NEED TO DO A LITTLE BIT OF WORK FIRST TO IDENTIFY OUTLIERS. */


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/static FS and SS partialled out for R", text replace
***********************************************************************************************************************
* PARTIALLED OUT STATIC FIRST AND SECOND STAGE FOR R
***********************************************************************************************************************
/* The aim of this block of code is to create a dataset with partialled out variables,
which can then be exported to R to apply the robust IV estimator to. */

preserve

// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* First generate partialled out variables for each of the models. */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* Now generate partialled out dep var, aid and aid iv. */
		qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double depvarres_`depvar', e
		qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double aidres_`depvar', e
		qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double ivres_`depvar', e

	} // end qui
} // end loop over models.
/* Keep only non-missing observations. */
keep if incflag == 1 & incflagsum >= 6
/* Keep only relevant variables. */
keep country ccode period periodid obsnum incflag depvarres_* aidres_* ivres_*
order country ccode period periodid obsnum incflag
/* Save data. */
sort obsnum
outsheet using "`ouranalysis'/Outliers/StaticFSandSS.csv", comma replace
/* Check that partialled out regressions still work and that we have everything we need: */
foreach depvar in hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid

		/* Now check results hold with partialled out variables. */
		ivreg2 depvarres_`depvar' (aidres_`depvar' = ivres_`depvar'), first
		// results checked and OK.
}
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
// 
timer clear
restore
log close
*/


/*
capture log close
log using "`ouranalysis'/Result logs/dynamic FS and SS partialled out for R", text replace
***********************************************************************************************************************
* PARTIALLED OUT DYNAMIC FIRST AND SECOND STAGE FOR R
***********************************************************************************************************************
/* Same for dynamic model. */

preserve

// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* First generate partialled out variables for each of the models. */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* Now generate partialled out dep var, lag dep var, aid and aid iv. */
		qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|mlagdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double depvarres_`depvar', e
		qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|mlagdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double aidres_`depvar', e
		qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|mlagdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double ivres_`depvar', e
		qui: xi: xtreg lagdepvar i.ccode|mdepvar i.ccode|mlagdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6, fe
		predict double lagdepvarres_`depvar', e

	} // end qui
} // end loop over models.
/* Keep only non-missing observations. */
keep if incflag == 1 & incflagsum >= 6
/* Keep only relevant variables. */
keep country ccode period periodid obsnum incflag depvarres_* aidres_* ivres_* lagdepvarres_*
order country ccode period periodid obsnum incflag
/* Save data. */
sort obsnum
outsheet using "`ouranalysis'/Outliers/DynamicFSandSS.csv", comma replace
/* Check that partialled out regressions still work and that we have everything we need: */
foreach depvar in hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid

		/* Now check results hold with partialled out variables. */
		ivreg2 depvarres_`depvar' lagdepvarres_`depvar' (aidres_`depvar' = ivres_`depvar'), first
		// results checked and OK.
}
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
// 
timer clear
restore
log close
*/


*****************************************************************************
* IDENTIFICATION OF OUTLIERS
*****************************************************************************
/* To see how we've identified outliers, use the two .csv files just created
and run rivscript8 in R. The Mahalanobis distances this yields are saved in
distancesdata.dta and dynamicdistancesdata.dta, which are analysed in analysisdistances.do. */


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 8 row 9 drop outliers", text replace
*******************************************************************************************************
* TABLE 8 ROW 9: DROP OUTLIERS
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" /// 
		& country != "Burundi" & country != "Central African Rep." & country != "Chad" & country != "Congo, Dem. Rep." ///
		& country != "Jordan" & country != "Madagascar" & country != "Mauritania")          
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 9 row 1 FD", text replace
*******************************************************************************************************
* TABLE 9 ROW 1: FIRST-DIFFERENCED MODEL
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = d.netaidGDP
replace aidiv = d.netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = d.`depvar'

		/* Keep sample fixed: */
		capture drop incflag
		gen incflag=(depvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Stick to only including countries with at least 6 observations.*/

		// construct sample means for all variables:
		capture drop mdepvar maid minst 
		capture drop mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6

		/* static FD CCE IV regression: */
		eststo FDCCEIVstat_`depvar': xi: ivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS HERE USING ESTTAB. */
display "STATIC CCE IV"
esttab FDCCEIVstat_hconsGDP FDCCEIVstat_gconsGDP FDCCEIVstat_tconsGDPsum FDCCEIVstat_grcapformGDP FDCCEIVstat_exportGDP FDCCEIVstat_importGDP /// 
FDCCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table 9 row 2 FD without outliers", text replace
*******************************************************************************************************
* TABLE 9 ROW 2: FIRST-DIFFERENCED MODEL WITHOUT OUTLIERS
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
sort ccode periodid
xtset ccode periodid
replace aidvar = d.netaidGDP
replace aidiv = d.netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = d.`depvar'

		/* Keep sample fixed. */
		capture drop incflag
		gen incflag=(depvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" ///
		& country != "Burundi" & country != "Central African Rep." & country != "Chad" & country != "Congo, Dem. Rep." ///
		& country != "Jordan" & country != "Madagascar" & country != "Mauritania")          
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Stick to only including countries with at least 6 observations.*/

		// construct sample means for all variables:
		capture drop mdepvar maid minst 
		capture drop mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6

		/* static FD CCE IV regression: */
		eststo FDCCEIVstat_`depvar': xi: ivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS HERE USING ESTTAB. */
display "STATIC CCE IV"
esttab FDCCEIVstat_hconsGDP FDCCEIVstat_gconsGDP FDCCEIVstat_tconsGDPsum FDCCEIVstat_grcapformGDP FDCCEIVstat_exportGDP FDCCEIVstat_importGDP /// 
FDCCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 2 no FEs", text replace
*************************************************************
* TABLE B.1 IN APPENDIX ROW 2: DROP FIXED EFFECTS
*************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static IV regression without FEs: */
		eststo IVstat_`depvar': xi: ivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, cluster(ccode) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic IV regression without FEs: */
		eststo IVdyn_`depvar': xi: ivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, cluster(ccode) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC IV (NO FEs)"
esttab IVstat_hconsGDP IVstat_gconsGDP IVstat_tconsGDPsum IVstat_grcapformGDP IVstat_exportGDP IVstat_importGDP IVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC IV (NO FEs)"
esttab IVdyn_hconsGDP IVdyn_gconsGDP IVdyn_tconsGDPsum IVdyn_grcapformGDP IVdyn_exportGDP IVdyn_importGDP IVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 3 no aid before independence", text replace
******************************************************************
* TABLE B.1 IN APPENDIX ROW 3: EXCLUDE AID BEFORE INDEPENDENCE
******************************************************************
// change aid variables:
replace aidvar = indep_netaidGDP
replace aidiv = indep_netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 4 lagged and current aid", text replace
******************************************************************
* TABLE B.1 IN APPENDIX ROW 4: LAGGED AND CURRENT AID
******************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP
capture drop lagaidvar
capture drop lagaidiv
gen double lagaidvar = L.netaidGDP
gen double lagaidiv = L.netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & lagaidvar<. & lagaidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Now need at least 8 time series observations for estimation. Each country has constant (FE) plus 6 factor loadings 
		(aid and lag, instrument and lag, dep var and lag dep var). */

		// construct sample means for all variables:
		capture drop mdepvar 
		capture drop mlagaid mlaginst 
		capture drop mlagdepvar 
		capture drop maid minst
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 8
		bysort periodid: egen double mlagaid=mean(lagaidvar) if incflag == 1 & incflagsum >= 8
		bysort periodid: egen double mlaginst=mean(lagaidiv) if incflag == 1 & incflagsum >= 8
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 8
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 8
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 8

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar lagaidvar = aidiv lagaidiv) i.ccode|mdepvar i.ccode|mlagaid i.ccode|mlaginst /// 
		i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 8, fe cluster(ccode) endog(aidvar lagaidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar lagaidvar = aidiv lagaidiv) i.ccode|mdepvar i.ccode|mlagaid i.ccode|mlagdepvar /// 
		i.ccode|mlaginst i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 8, fe cluster(ccode) endog(aidvar lagaidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar lagaidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagaidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 5 final year values dep var", text replace
****************************************************************************
* TABLE B.1 IN APPENDIX ROW 5: FINAL YEAR VALUES FOR DEPENDENT VARIABLES
****************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'_end"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'_end // this is the only line that needs to change in the code.
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 6 exclude first 2 periods", text replace
****************************************************************************
* TABLE B.1 IN APPENDIX ROW 6: EXCLUDE FIRST TWO PERIODS
****************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 7 exclude first 3 periods", text replace
****************************************************************************
* TABLE B.1 IN APPENDIX ROW 7: EXCLUDE FIRST THREE PERIODS
****************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76" ///
		& period != "1977-79")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 8 instr6073 exclude 2 periods", text replace
************************************************************************************************
* TABLE B.1 IN APPENDIX ROW 8: INSTRUMENT BASED ON 1960-73 INITIAL SHARES, EXCLUDE TWO PERIODS
************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6073GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76" ///
		& period != "1977-79")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 9 instr6073 exclude 3 periods", text replace
************************************************************************************************
* TABLE B.1 IN APPENDIX ROW 9: INSTRUMENT BASED ON 1960-73 INITIAL SHARES, EXCLUDE THREE PERIODS
************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6073GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76" ///
		& period != "1977-79" & period != "1980-82")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 10 instr6076", text replace
************************************************************************************************
* TABLE B.1 IN APPENDIX ROW 10: INSTRUMENT BASED ON 1960-76 INITIAL SHARES
************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6076GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 11 instr6076 exclude 1 period", text replace
************************************************************************************************
* TABLE B.1 IN APPENDIX ROW 11: INSTRUMENT BASED ON 1960-76 INITIAL SHARES, EXCLUDE ONE PERIOD
************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6076GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76" ///
		& period != "1977-79")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B1 appendix row 12 instr6076 exclude 2 periods", text replace
************************************************************************************************
* TABLE B.1 IN APPENDIX ROW 12: INSTRUMENT BASED ON 1960-76 INITIAL SHARES, EXCLUDE TWO PERIODS
************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6076GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" & period != "1971-73" & period != "1974-76" ///
		& period != "1977-79" & period != "1980-82")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B2 appendix row 1 log dep vars", text replace
*******************************************************************************************************
* TABLE B.2 IN APPENDIX ROW 1: LOG DEPENDENT VARIABLES
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist lntconsGDPsum lncigGDPsum {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & lnGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Now need at least 7 time series observations for estimation when XS mean of log Y is included. 
		Each country has constant (FE) plus 5 factor loadings (aid, instrument, dep var, lag dep var, and log Y). 
		Without cross-sectional mean of log Y 6 observations is fine. */
		
		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop mlogy
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlogy=mean(lnGDPcurrentUSD) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression without cross-sectional mean of logY: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) lnGDPcurrentUSD i.ccode|mdepvar i.ccode|maid i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		/* dynamic CCE IV regression without cross-sectional mean of logY: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) lnGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1 & incflagsum >= 6, /// 
		fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop mlogy
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlogy=mean(lnGDPcurrentUSD) if incflag == 1 & incflagsum >= 7

		/* static CCE IV regression with cross-sectional mean of logY: */
		eststo CCEIVstatXSm_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) lnGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|minst i.ccode|mlogy if incflag==1 & incflagsum >= 7 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		/* dynamic CCE IV regression with cross-sectional mean of logY: */
		eststo CCEIVdynXSm_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) lnGDPcurrentUSD ///
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst i.ccode|mlogy /// 
		if incflag==1 & incflagsum >= 7, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS HERE USING ESTTAB. */
// Static:
esttab CCEIVstat_lntconsGDPsum CCEIVstat_lncigGDPsum CCEIVstatXSm_lntconsGDPsum CCEIVstatXSm_lncigGDPsum, /// 
keep(aidvar lnGDPcurrentUSD) mtitles("Log(C+G)" "Log(C+I+G)" "Log(C+G)" "Log(C+I+G)") `exportstataIVrob'
// Dynamic:
esttab CCEIVdyn_lntconsGDPsum CCEIVdyn_lncigGDPsum CCEIVdynXSm_lntconsGDPsum CCEIVdynXSm_lncigGDPsum, /// 
keep(aidvar lagdepvar lnGDPcurrentUSD) mtitles("Log(C+G)" "Log(C+I+G)" "Log(C+G)" "Log(C+I+G)") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B2 appendix row 2 log dep vars without outliers", text replace
*******************************************************************************************************
* TABLE B.2 IN APPENDIX ROW 2: LOG DEPENDENT VARIABLES WITHOUT OUTLIERS
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist lntconsGDPsum lncigGDPsum {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & lnGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" ///
		& country != "Burundi" & country != "Central African Rep." & country != "Chad" & country != "Congo, Dem. Rep." ///
		& country != "Jordan" & country != "Madagascar" & country != "Mauritania")          
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Now need at least 7 time series observations for estimation when XS mean of log Y is included. 
		Each country has constant (FE) plus 5 factor loadings (aid, instrument, dep var, lag dep var, and log Y). 
		Without cross-sectional mean of log Y 6 observations is fine. */
		
		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop mlogy
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlogy=mean(lnGDPcurrentUSD) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression without cross-sectional mean of logY: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) lnGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		/* dynamic CCE IV regression without cross-sectional mean of logY: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) lnGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop mlogy
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlogy=mean(lnGDPcurrentUSD) if incflag == 1 & incflagsum >= 7

		/* static CCE IV regression with cross-sectional mean of logY: */
		eststo CCEIVstatXSm_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) lnGDPcurrentUSD ///
		i.ccode|mdepvar i.ccode|maid i.ccode|minst i.ccode|mlogy if incflag==1 & incflagsum >= 7 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		/* dynamic CCE IV regression with cross-sectional mean of logY: */
		eststo CCEIVdynXSm_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) lnGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst i.ccode|mlogy /// 
		if incflag==1 & incflagsum >= 7, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS HERE USING ESTTAB. */
// Static:
esttab CCEIVstat_lntconsGDPsum CCEIVstat_lncigGDPsum CCEIVstatXSm_lntconsGDPsum CCEIVstatXSm_lncigGDPsum, /// 
keep(aidvar lnGDPcurrentUSD) mtitles("Log(C+G)" "Log(C+I+G)" "Log(C+G)" "Log(C+I+G)") `exportstataIVrob'
// Dynamic:
esttab CCEIVdyn_lntconsGDPsum CCEIVdyn_lncigGDPsum CCEIVdynXSm_lntconsGDPsum CCEIVdynXSm_lncigGDPsum, /// 
keep(aidvar lagdepvar lnGDPcurrentUSD) mtitles("Log(C+G)" "Log(C+I+G)" "Log(C+G)" "Log(C+I+G)") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B3 appendix row 3 control for inv GDP", text replace
*******************************************************************************************************
* TABLE B.3 IN APPENDIX ROW 3: CONTROL FOR INVERSE GDP
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & invGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B3 appendix row 4 control for inv GDP no outliers", text replace
*******************************************************************************************************
* TABLE B.3 IN APPENDIX ROW 4: CONTROL FOR INVERSE GDP NO OUTLIERS
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & invGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" ///
		& country != "Burundi" & country != "Central African Rep." & country != "Chad" & country != "Congo, Dem. Rep." ///
		& country != "Jordan" & country != "Madagascar" & country != "Mauritania")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) invGDPcurrentUSD ///
		i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		if incflag==1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B3 appendix row 5 control for inv GDP and XSmean", text replace
*******************************************************************************************************
* TABLE B.3 IN APPENDIX ROW 5: CONTROL FOR INVERSE GDP AND ITS CROSS-SECTIONAL MEAN 
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & invGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Now need at least 7 time series observations for estimation. Each country has constant (FE) plus 5 factor loadings 
		(aid, instrument, dep var, lag dep var and inverse GDP). */

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop minvGDP
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minvGDP=mean(invGDPcurrentUSD) if incflag == 1 & incflagsum >= 7
		
		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|minst i.ccode|minvGDP if incflag==1 & incflagsum >= 7 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		i.ccode|minvGDP if incflag==1 & incflagsum >= 7, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Table B3 appendix row 6 control for inv GDP and XSmean no outliers", text replace
*******************************************************************************************************
* TABLE B.3 IN APPENDIX ROW 6: CONTROL FOR INVERSE GDP AND ITS CROSS-SECTIONAL MEAN, NO OUTLIERS
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	qui {
		display "DEPENDENT VARIABLE IS `depvar'"
		sort ccode periodid
		xtset ccode periodid
		replace depvar = `depvar'
		replace lagdepvar = L.depvar

		/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
		capture drop incflag
		gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & invGDPcurrentUSD <. /// 
		& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70" ///
		& country != "Burundi" & country != "Central African Rep." & country != "Chad" & country != "Congo, Dem. Rep." ///
		& country != "Jordan" & country != "Madagascar" & country != "Mauritania")
		capture drop incflagsum
		bysort ccode (period): egen incflagsum = sum(incflag)
		/* Now need at least 7 time series observations for estimation. Each country has constant (FE) plus 5 factor loadings 
		(aid, instrument, dep var, lag dep var and inverse GDP). */

		// construct sample means for all variables:
		capture drop mdepvar maid minst mlagdepvar
		capture drop minvGDP
		bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 7
		bysort periodid: egen double minvGDP=mean(invGDPcurrentUSD) if incflag == 1 & incflagsum >= 7
		
		/* static CCE IV regression: */
		eststo CCEIVstat_`depvar': xi: xtivreg2 depvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|minst i.ccode|minvGDP if incflag==1 & incflagsum >= 7 ///
		, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
		
		/* dynamic CCE IV regression: */
		eststo CCEIVdyn_`depvar': xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) invGDPcurrentUSD /// 
		i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
		i.ccode|minvGDP if incflag==1 & incflagsum >= 7, fe cluster(ccode) endog(aidvar) first savefirst small
		qui storeresult `e(_estimates_name)'
	} // end qui
}
/* DISPLAY RESULTS USING ESTTAB. */
// Static:
display "STATIC CCE IV"
esttab CCEIVstat_hconsGDP CCEIVstat_gconsGDP CCEIVstat_tconsGDPsum CCEIVstat_grcapformGDP CCEIVstat_exportGDP CCEIVstat_importGDP CCEIVstat_netimportGDP, /// 
keep(aidvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
// Dynamic:
display "DYNAMIC CCE IV"
esttab CCEIVdyn_hconsGDP CCEIVdyn_gconsGDP CCEIVdyn_tconsGDPsum CCEIVdyn_grcapformGDP CCEIVdyn_exportGDP CCEIVdyn_importGDP CCEIVdyn_netimportGDP ///
, keep(aidvar lagdepvar) mtitles("C" "G" "C+G" "I" "X" "M" "M-X") `exportstataIVrob'
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
timer clear
log close
*/


// Uncomment if you want to run this block of code.
/*
capture log close
log using "`ouranalysis'/Result logs/Figures B1 B2 appendix", text replace
*******************************************************************************************************
* FIGURES B.1 AND B.2 IN APPENDIX B.5: DROPPING COUNTRIES WITH MOST NEGATIVE RELATIVE REAL GDP CHANGES
*******************************************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Who are the 15 most negative relative change in GDP countries that will be dropped here? */
gsort +minrelRGDPchange +periodid
list country minrelRGDPchange minrelRGDPchangerank if periodid == 18 & minrelRGDPchangerank < 16

/* Set off loop (and time how long it takes). */
timer clear
timer on 1
foreach depvar of varlist hconsGDP gconsGDP tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP {
	/* First set up title for each graph: */
	if "`depvar'" == "hconsGDP" {
		local title "C"
	}
	else if "`depvar'" == "gconsGDP" {
		local title "G"
	}
	else if "`depvar'" == "tconsGDPsum" {
		local title "C + G"
	}
	else if "`depvar'" == "grcapformGDP" {
		local title "I"
	}
	else if "`depvar'" == "exportGDP" {
		local title "X"
	}
	else if "`depvar'" == "importGDP" {
		local title "M"
	}
	else if "`depvar'" == "netimportGDP" {
		local title "M - X"
	}
	else {
		di as error "Cannot deal with this dependent variable"
		exit
	}
	foreach num of numlist 0(1)15 {
		qui {
			display "DEPENDENT VARIABLE IS `depvar'"
			sort ccode periodid
			xtset ccode periodid
			replace depvar = `depvar'
			replace lagdepvar = L.depvar

			/* Keep sample fixed to be the one used in the dynamic CCE IV regressions. */
			capture drop incflag
			gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. & minrelRGDPchangerank > `num' /// 
			& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
			capture drop incflagsum
			bysort ccode (period): egen incflagsum = sum(incflag)

			// construct sample means for all variables:
			capture drop mdepvar maid minst mlagdepvar
			bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1 & incflagsum >= 6
			bysort periodid: egen double maid=mean(aidvar) if incflag == 1 & incflagsum >= 6
			bysort periodid: egen double minst=mean(aidiv) if incflag == 1 & incflagsum >= 6
			bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1 & incflagsum >= 6

			/* static CCE IV regression: */
			eststo `depvar'_`num': xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1 & incflagsum >= 6 ///
			, fe cluster(ccode) small
		
			/* dynamic CCE IV regression: */
			xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst /// 
			if incflag==1 & incflagsum >= 6, fe cluster(ccode) small
			eststo dyn_`depvar'_`num': nlcom (LR: _b[aidvar]/(1-_b[lagdepvar])), post	
		} // end qui
	} // end loop over number of countries to drop.

	/* Create graph for STATIC model. */
	coefplot `depvar'_0 || `depvar'_1 || `depvar'_2 || `depvar'_3 || `depvar'_4 || `depvar'_5 || `depvar'_6 || `depvar'_7 || `depvar'_8 || `depvar'_9 /// 
	|| `depvar'_10 || `depvar'_11 || `depvar'_12 || `depvar'_13 || `depvar'_14 || `depvar'_15 ///
	, keep(aidvar) bycoefs vertical recast(line) ciopts(recast(rline)lpattern(dash)) levels(90) xlabel(1 "0" 6 "5" 11 "10" 16 "15") /// 
	yscale(range(-1 2.5)) ylabel(-1 -0.5 0 0.5 1 1.5 2 2.5) title("`title'")
	/* Export graph. */
	graph export "`aidinstrument'/Paper/Figures/negdRGDP/Static CCE IV/`depvar'.pdf", replace
	
	/* Create graph for DYNAMIC model. */
	coefplot dyn_`depvar'_0 || dyn_`depvar'_1 || dyn_`depvar'_2 || dyn_`depvar'_3 || dyn_`depvar'_4 || dyn_`depvar'_5 || dyn_`depvar'_6 || dyn_`depvar'_7  ///
	|| dyn_`depvar'_8 || dyn_`depvar'_9 || dyn_`depvar'_10 || dyn_`depvar'_11 || dyn_`depvar'_12 || dyn_`depvar'_13 || dyn_`depvar'_14 || dyn_`depvar'_15 ///
	, keep(LR) bycoefs vertical recast(line) ciopts(recast(rline)lpattern(dash)) levels(90) xlabel(1 "0" 6 "5" 11 "10" 16 "15") /// 
	yscale(range(-1 3)) ylabel(-1 -0.5 0 0.5 1 1.5 2 2.5 3) title("`title'")
	/* Export graph. */
	graph export "`aidinstrument'/Paper/Figures/negdRGDP/Dyn CCE IV/`depvar'.pdf", replace

} // end loop over dependent variables.
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
// minutes.
timer clear
log close
*/
